/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-10 下午6:06
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.adapter.consulation;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.consulation.ConsulationComment;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.view.CircleImageView;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
*description:资讯详情Adapter
*autour:guanluocang
*date:17/7/11 09:44
*/
public class ConsulationCommentAdapter extends RecyclerView.Adapter<ConsulationCommentAdapter.ConsulationCommentViewHolder> {
    private List<ConsulationComment.DataBean> mConsulationCommentList = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;
    private OnFabulousListener mOnFabulousListener = null;
    private OnDeleteListener mOnDeleteListener = null;

    public ConsulationCommentAdapter(Context context,List<ConsulationComment.DataBean> consulationCommentList,OnFabulousListener onFabulousListener,OnDeleteListener onDeleteListener) {
        this.mContext = context;
        this.mOnDeleteListener = onDeleteListener;
        this.mOnFabulousListener = onFabulousListener;
        this.mConsulationCommentList = consulationCommentList;
        inflater = LayoutInflater.from(context);
    }

    public void refresh_newHots() {
        notifyDataSetChanged();
    }

    @Override
    public ConsulationCommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ConsulationCommentViewHolder consulationCommentViewHolder = new ConsulationCommentViewHolder(inflater.inflate(R.layout.layout_consulation_comment, parent, false));
        return consulationCommentViewHolder;
    }

    @Override
    public void onBindViewHolder(ConsulationCommentViewHolder holder, final int position) {
        holder.tv_fabulousNum.setText(mConsulationCommentList.get(position).getCount_praise() + "");
        x.image().bind(holder.iv_photo, Config.pic_base_url + mConsulationCommentList.get(position).getPhoto());
        holder.tv_userName.setText(mConsulationCommentList.get(position).getName());
        holder.tv_addTime.setText(mConsulationCommentList.get(position).getAdd_time());
        holder.tv_content.setText(mConsulationCommentList.get(position).getContent());
        //"is_praise":1   //赞的状态：0-未赞，1-已赞
        if(mConsulationCommentList.get(position).getIs_praise() == 1){
            holder.iv_fabulous.setImageResource(R.drawable.ic_fabulous_pressed);
        }else{
            holder.iv_fabulous.setImageResource(R.drawable.ic_fabulous);
        }
        //点赞
        holder.iv_fabulous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnFabulousListener.onFabulous(position);
            }
        });
        //删除
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnDeleteListener.onDelete(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mConsulationCommentList.size();
    }

    //删除
    public void removeItem(int position) {
        mConsulationCommentList.remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    class ConsulationCommentViewHolder extends RecyclerView.ViewHolder {
        //点赞数量
        TextView tv_fabulousNum;
        //头像
        CircleImageView iv_photo;
        //用户姓名
        TextView tv_userName;
        //评论时间
        TextView tv_addTime;
        //评论内容
        TextView tv_content;
        //删除
        TextView tv_delete;
        //点赞
        ImageView iv_fabulous;

        public ConsulationCommentViewHolder(View view) {
            super(view);
            tv_fabulousNum = (TextView) view.findViewById(R.id.tv_fabulousNum);
            tv_userName = (TextView) view.findViewById(R.id.tv_userName);
            tv_addTime = (TextView) view.findViewById(R.id.tv_addTime);
            tv_content = (TextView) view.findViewById(R.id.tv_content);
            tv_delete = (TextView) view.findViewById(R.id.tv_delete);
            iv_photo = (CircleImageView) view.findViewById(R.id.iv_photo);
            iv_fabulous = (ImageView) view.findViewById(R.id.iv_fabulous);

        }
    }

    public static interface OnFabulousListener{
        void onFabulous(int position);
    }
    public static interface OnDeleteListener{
        void onDelete(int position);
    }
}
